/**
 *
 */
package de.cbf.cam.dsa.item;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.ui.IPartListener2;
import org.eclipse.ui.IPartService;
import org.eclipse.ui.IStartup;
import org.eclipse.ui.IWindowListener;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

import de.cbf.cam.dsa.item.act.ActivatorItem;
import de.cbf.cam.dsa.item.ui.view.ViewItemEditor;

/**
 * @author Alti
 *
 */
public class StartUpListener implements IStartup {
	private static final Log logger = LogFactory.getLog(StartUpListener.class);

	private IPartListener2 listener = new EditItemViewActivatedListener();
	/**
	 *
	 */
	public StartUpListener() {

	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IStartup#earlyStartup()
	 */
	@Override
	public void earlyStartup() {
		logger.info("Early StartUp for Item Editor!");
		final IWorkbench workbench = PlatformUI.getWorkbench();
        logger.info("Workbench=" + workbench);
        //add listener to existing windows
        IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();
        for (int i = 0; i < windows.length; i++) {
			IWorkbenchWindow window = windows[i];
			IPartService partService = window.getPartService();
			partService.addPartListener(listener);
		}
        //add listener if windows are opened
        workbench.addWindowListener(new IWindowListener() {

			@Override
			public void windowOpened(IWorkbenchWindow window) {
				IPartService partService = window.getPartService();
				partService.addPartListener(listener);
			}

			@Override
			public void windowDeactivated(IWorkbenchWindow window) {
			}

			@Override
			public void windowClosed(IWorkbenchWindow window) {
				IPartService partService = window.getPartService();
				partService.removePartListener(listener);
			}

			@Override
			public void windowActivated(IWorkbenchWindow window) {
			}
		});
	}

	protected class EditItemViewActivatedListener implements IPartListener2{

		/* (non-Javadoc)
		 * @see org.eclipse.ui.IPartListener2#partActivated(org.eclipse.ui.IWorkbenchPartReference)
		 */
		@Override
		public void partActivated(IWorkbenchPartReference partRef) {
			if(ViewItemEditor.ID.equals(partRef.getId())){
				logger.debug("view to edit Items activated");
				ViewItemEditor view = (ViewItemEditor) partRef.getPart(false);
				ControllerEditItem controllerItem = ActivatorItem.getDefault().getControllerEditItem();
				controllerItem.setCompositeEditItem(view.getCompositeEditItem());
				ControllerEditItems controllerItems = ActivatorItem.getDefault().getControllerEditItems();
				controllerItems.setCompositeEditItems(view.getCompositeEditItems());
			}
		}

		/* (non-Javadoc)
		 * @see org.eclipse.ui.IPartListener2#partBroughtToTop(org.eclipse.ui.IWorkbenchPartReference)
		 */
		@Override
		public void partBroughtToTop(IWorkbenchPartReference partRef) {
		}

		/* (non-Javadoc)
		 * @see org.eclipse.ui.IPartListener2#partClosed(org.eclipse.ui.IWorkbenchPartReference)
		 */
		@Override
		public void partClosed(IWorkbenchPartReference partRef) {
		}

		/* (non-Javadoc)
		 * @see org.eclipse.ui.IPartListener2#partDeactivated(org.eclipse.ui.IWorkbenchPartReference)
		 */
		@Override
		public void partDeactivated(IWorkbenchPartReference partRef) {
			if(ViewItemEditor.ID.equals(partRef.getId())){
				logger.debug("view to edit Items deactivated");
				ViewItemEditor view = (ViewItemEditor) partRef.getPart(false);
				ControllerEditItem controllerItem = ActivatorItem.getDefault().getControllerEditItem();
				controllerItem.removeCompositeEditItem();
				ControllerEditItems controllerItems = ActivatorItem.getDefault().getControllerEditItems();
				controllerItems.removeCompositeEditItems(view.getCompositeEditItems());
			}
		}

		/* (non-Javadoc)
		 * @see org.eclipse.ui.IPartListener2#partOpened(org.eclipse.ui.IWorkbenchPartReference)
		 */
		@Override
		public void partOpened(IWorkbenchPartReference partRef) {
		}

		/* (non-Javadoc)
		 * @see org.eclipse.ui.IPartListener2#partHidden(org.eclipse.ui.IWorkbenchPartReference)
		 */
		@Override
		public void partHidden(IWorkbenchPartReference partRef) {
		}

		/* (non-Javadoc)
		 * @see org.eclipse.ui.IPartListener2#partVisible(org.eclipse.ui.IWorkbenchPartReference)
		 */
		@Override
		public void partVisible(IWorkbenchPartReference partRef) {
			// TODO Auto-generated method stub

		}

		/* (non-Javadoc)
		 * @see org.eclipse.ui.IPartListener2#partInputChanged(org.eclipse.ui.IWorkbenchPartReference)
		 */
		@Override
		public void partInputChanged(IWorkbenchPartReference partRef) {
			// TODO Auto-generated method stub

		}
	}
}
